
import request from './request.js'
import api from './interface.js'
import {
	pathToBase64,
	base64ToPath
} from 'image-tools'
const formatData = function(formatStr) {

	const date = new Date();
	const year = date.getFullYear().toString().padStart(4, '0');
	const month = (date.getMonth() + 1).toString().padStart(2, '0');
	const day = date.getDate().toString().padStart(2, '0');
	const hour = date.getHours().toString().padStart(2, '0');
	const minute = date.getMinutes().toString().padStart(2, '0');
	const second = date.getSeconds().toString().padStart(2, '0');

	return year + month + day;
}
const upload = {

		chooseImage:function() {
			const data1 = formatData()
			const data2 = new Date().valueOf() + ''
			const key = 'images/' + data1 + '/' + data2 + '.png'
			return new Promise((resolve, reject) => {
			wx.chooseImage({
				count: 1,
				success: res => {
					const tempFilePaths = res.tempFilePaths
					const filePath = tempFilePaths[0]
			
	
					console.log(key);
					const uploadToken = uni.getStorageSync('upToken')
					const formData = {
						token: uploadToken,
						key
					}
					wx.uploadFile({
						url: 'https://up-z2.qiniup.com',
						filePath: filePath,
						name: 'file',
						formData: formData,
						header: {
							'Content-Type': 'multipart/form-data'
						},
						success: res => {
							
							console.log('上传成功', res)
							
							
							resolve({"image" : key,
									"showImage":tempFilePaths})
						},
						fail: error => {
							console.log('上传失败', error)
							reject(error)
						}
					})
				}
			})
			})
			
	}
}
export default upload